<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------

namespace app\api\home;

use app\common\controller\Api;
use app\common\model\Group as groupModel;
use app\common\model\GroupCategories as cateModel;
use app\common\model\GroupPosts as postModel;
use think\Db;

class Group extends Api
{
    protected $groupModel;

    /**
     * 星球相关接口
     *
     *
     */
    public function __construct()
    {
        parent::__construct();
        $this->groupModel = new groupModel();
    }

    /**
     * App首页API
     * @author
     */
    public function getCoverList()
    {
        $recommend = $this->groupModel->groupList(0, 6, 2);
        //$hot = $this->groupModel->groupList(0, 6, 1);
        $res = array(
            'status' => 1,
            'code' => 0,
        );
        $res['data']['recommend'] = $recommend;
        //$res['data']['hot'] = $hot;
        echo json_encode($res);
        exit();
    }

    /**
     * App首页API
     * @author
     */
    public function getHotList()
    {
        $postData = $this->requestData;
        $page = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
        //$recommend = $this->groupModel->groupList(0, 6, 2);
        $hot = $this->groupModel->groupList($page, 6, 1);
        $res = array(
            'status' => 1,
            'code' => 0,
        );
        $res['data']['hot'] = $hot;
        //$res['data']['hot'] = $hot;
        echo json_encode($res);
        exit();
    }


    /**
     * 星球首页
     * @author
     */
    public function groupHome()
    {
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId'] > 0) {
            $postModel = new postModel();
            $groupInfo = $this->groupModel->groupInfo($postData['groupId']);
            $postList = $postModel->postList($postData['groupId'], 0, 6);
            if ($groupInfo) {
                $data['groupInfo'] = $groupInfo;
                $data['postList'] = $postList;
                $result = array(
                    'status' => 1,
                    'data' => $data,
                    'code' => 0,
                );
            } else {
                $result = array(
                    'status' => 0,
                    'data' => 'no group',
                    'code' => 1,
                );
            }
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 获取主题列表
     * @author
     */
    public function getPostList(){
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId'] > 0) {
            $postModel = new postModel();
            $page = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
            $postList = $postModel->postList($postData['groupId'], $page, 6);
            if($postList){
                $result = array(
                    'status' => 1,
                    'data' => $postList,
                    'code' => 0,
                );
            }else{
                $result = array(
                    'status' => 1,
                    'data' => [],
                    'code' => 0,
                );
            }
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 获取班级信息
     * @author
     */
    public function groupInfo()
    {
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId']) {
            $postModel = new postModel();
            $groupInfo = $this->groupModel->groupInfo($postData['groupId']);
            if ($groupInfo) {
                $groupInfo['essences'] = $postModel->where('group_id', $postData['groupId'])->where('disabled', 1)->where('is_essence', 1)->count('id');
                $vnsInfo = Db::name('currency')->where('id',2)->field('last_price')->find();
                $groupInfo['vnsPrice'] = $vnsInfo['last_price'];
                $groupInfo['cday'] = ceil((time() - strtotime($groupInfo['ctime'])) / 86400);
            }
            $postList = $postModel->postList($postData['groupId'], 0, 6);
            if ($groupInfo) {
                $data['groupInfo'] = $groupInfo;
                $data['postList'] = $postList;
                $result = array(
                    'status' => 1,
                    'data' => $data,
                    'code' => 1,
                );
            } else {
                $result = array(
                    'status' => 0,
                    'data' => 'no group',
                    'code' => 1,
                );
            }

        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 只获取班级信息
     * @author
     */
    public function groupOnlyInfo()
    {
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId']) {
            $groupInfo = $this->groupModel->groupInfo($postData['groupId']);
            if ($groupInfo) {
                $groupInfo;
                $result = array(
                    'status' => 1,
                    'data' => $groupInfo,
                    'code' => 1,
                );
            } else {
                $result = array(
                    'status' => 0,
                    'data' => 'no group',
                    'code' => 1,
                );
            }

        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 创建星球
     * @author
     */
    public function createGroup()
    {
        $postData = $this->requestData;
        $resVaild = $this->validate($postData, 'Group.register');
        if ($resVaild !== true) {
            $result = array(
                'status' => 0,
                'data' => 'create failed',
            );
            if ($resVaild == '1') {
                $result['code'] = 5;
            } elseif ($resVaild == '2') {
                $result['code'] = 6;
            } elseif ($resVaild == '3') {
                $result['code'] = 7;
            } elseif ($resVaild == '4') {
                $result['code'] = 8;
            } elseif ($resVaild == '5') {
                $result['code'] = 9;
            } elseif ($resVaild == '6') {
                $result['code'] = 10;
            }
            echo json_encode($result);
            exit;
        }
        $res = $this->groupModel->createGroup($postData);
        if ($res) {
            $result = array(
                'status' => 1,
                'data' => 'create success',
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 0,
                'data' => 'create failed',
                'code' => 4,
            );
        }
        echo json_encode($result);
        exit;

    }


    /**
     * 查看星球用户关系
     * @author
     */
    public function groupUserInfo()
    {
        $postData = $this->requestData;
        $res = array(
            'status' => 1,
        );
        if (isset($postData['groupId']) && $postData['groupId'] > 0 && isset($postData['userId']) && $postData['userId'] > 0) {
            $groupUser = $this->groupModel->groupUser($postData['groupId'], $postData['userId']);
            if ($groupUser) {
                $res['is_member'] = 1;
            } else {
                $res['is_member'] = 0;
            }
        } else {
            $res['is_member'] = 0;
        }
        echo json_encode($res);
        exit();
    }

    /**
     * 我的星球
     * @author
     */
    public function myGroup()
    {
        $postData = $this->requestData;
        if (isset($postData['userId']) && $postData['userId'] > 0) {
            $nowPage = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
            $myGroup = $this->groupModel->myGroup($postData['userId'],$nowPage);
            $result = array(
                'status' => 1,
                'data' => $myGroup ? $myGroup : array(),
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 我创建的班级
     * @author
     */
    public function myCreate()
    {
        $postData = $this->requestData;
        if (isset($postData['userId']) && $postData['userId'] > 0) {
            $nowPage = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
            $myGroup = $this->groupModel->myCreate($postData['userId'],$nowPage);
            $result = array(
                'status' => 1,
                'data' => $myGroup ? $myGroup : array(),
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 搜索星球
     * @author
     */
    public function searchGroup()
    {
        $postData = $this->requestData;
        if ($postData['searchStr'] != '') {
            $userGroup = $this->groupModel->getUserGroup($postData['userId']);
            $postData['searType'] = isset($postData['searType']) ? $postData['searType'] : 0;
            if($postData['searType'] == 0 || $postData['searType'] == 1){
                $resultGroup = $this->groupModel->searchGroup($postData['searchStr'], $userGroup, $postData['type'], 0, 4);
            }
            if($postData['searType'] == 0 || $postData['searType'] == 2) {
                if ($postData['type'] == 0) {
                    $postModel = new postModel();
                    $resultPost = $postModel->searchPost($postData['searchStr'], $userGroup, $postData['type'], 0, 4);
                }
            }
            $resultSearch['post'] = isset($resultPost) ? $resultPost : array();
            $resultSearch['group'] = isset($resultGroup) ? $resultGroup : array();
            $result = array(
                'status' => 1,
                'data' => $resultSearch ? $resultSearch : array(),
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 加入星球
     * @author
     */
    public function addGroup()
    {
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId'] > 0 && isset($postData['userId']) && $postData['userId'] > 0) {
            $tradePwd = isset($postData['tradePwd']) ? $postData['tradePwd'] : '';
            $res = $this->groupModel->insertGroupUser($postData['groupId'], $postData['userId'], $tradePwd,'',$postData['currencyId']);
            if ($res == 1) {
                $result = array(
                    'status' => 1,
                    'data' => 'insertGroup success',
                    'code' => 0,
                );
            } else if($res == -1){
                $result = array(
                    'status' => 0,
                    'data' => 'not sufficient funds',
                    'code' => 5,
                );
            }else if($res == -2){
                $result = array(
                    'status' => 0,
                    'data' => 'tradepwd wrong',
                    'code' => 6,
                );
            }else {
                $result = array(
                    'status' => 0,
                    'data' => 'insertGroup error',
                    'code' => 4,
                );
            }
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 获取班级配置
     * @author
     */
    public function getClassSet()
    {
        $minPrice = Db::name('GroupConfig')->where("`key`='min_price'")->find();
        $maxPrice = Db::name('GroupConfig')->where("`key`='max_price'")->find();
        $result = array(
            'status' => 1,
            'code' => 1,
        );
        $data['minPrice'] = $minPrice['value'];
        $data['maxPrice'] = $maxPrice['value'];
        $result['data'] = $data;
        echo json_encode($result);
        exit();
    }

    /**
     * 获取分类列表
     * @author
     */
    public function getClassType()
    {
        $cateModel = new cateModel();
        $cateList = $cateModel->getList();
        $result = array(
            'status' => 1,
            'data' => $cateList,
            'code' => 1,
        );
        echo json_encode($result);
        exit();
    }

    /**
     * 精华帖
     * @author
     */
    public function groupEssences()
    {
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId'] > 0) {
            $postModel = new postModel();
            $page = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
            $postList = $postModel->postList($postData['groupId'], $page, 6, true);
            if ($postList) {
                $result = array(
                    'status' => 1,
                    'data' => $postList,
                    'code' => 0,
                );
            } else {
                $result = array(
                    'status' => 0,
                    'data' => 'no posts',
                    'code' => 1,
                );
            }
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 获取成员列表
     * @author
     */
    public function getMemberList(){
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId'] > 0) {
            $page = isset($postData['nowPage']) ? $postData['nowPage'] : 0;
            $memberList = $this->groupModel->getMemberList($postData['groupId'],$page);
            $result = array(
                'status' => 1,
                'data' => $memberList,
                'code' => 0,
            );
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

    /**
     * 退出班级
     * @author
     */
    public function quitGroup(){
        $postData = $this->requestData;
        if (isset($postData['groupId']) && $postData['groupId'] > 0 && isset($postData['userId']) && $postData['userId'] > 0) {
            $res = $this->groupModel->quitGroup($postData['groupId'],$postData['userId']);
            if($res == 1){
                $result = array(
                    'status' => 1,
                    'data' => 'quit success',
                    'code' => 0,
                );
            }elseif($res == -1){
                $result = array(
                    'status' => 0,
                    'data' => 'is class header',
                    'code' => 4,
                );
            }else{
                $result = array(
                    'status' => 0,
                    'data' => 'quit failed',
                    'code' => 5,
                );
            }
        } else {
            $result = array(
                'status' => 0,
                'data' => 'param error',
                'code' => 1,
            );
        }
        echo json_encode($result);
        exit();
    }

}